﻿using AutoMapper;
using Modules;
using Modules.Attachment;
using Modules.Device;
using Modules.Manager;
using Modules.Project;
using PmSoft;
using System.ComponentModel.DataAnnotations;
using static Modules.Project.ProjectConstants;

namespace ManagementApi.Models.Project;

/// <summary>
/// 项目信息修改申请
/// </summary>
public class ProjectUpdateApplyForm
{
    /// <summary>
    /// 行业主管部门项目编号/住建：西藏自治区建筑市场监管平台项目编号
    ///</summary>
    [Display(Name = "行业主管部门项目编号/住建：西藏自治区建筑市场监管平台项目编号")]
    public string? IndustryPrjNum { get; set; }

    ///// <summary>
    ///// 施工许可证编号
    /////</summary>
    //[Display(Name = "施工许可证编号")]
    //[Required(ErrorMessage = "{0}不能为空")]
    //public string? BuilderLicences { get; set; }

    ///// <summary>
    ///// 施工许可证发证日期
    /////</summary>
    //[Display(Name = "施工许可证发证日期")]
    //[Required(ErrorMessage = "{0}不能为空")]
    //public DateTime? BuilderLicencesGrantDate { get; set; }

    ///// <summary>
    ///// 施工许可证有效期限
    /////</summary>
    //[Display(Name = "施工许可证有效期限")]
    //[Required(ErrorMessage = "{0}不能为空")]
    //public DateTime? BuilderLicencesTermDate { get; set; }

    /// <summary>
    /// 建设用地规划许可证编号。AES
    ///</summary>
    [Display(Name = "建设用地规划许可证编号")]
    public string? BuildPlanNum { get; set; }

    /// <summary>
    /// 建设工程规划许可证编号。AES
    ///</summary>
    [Display(Name = "建设工程规划许可证编号")]
    public string? PrjPlanNum { get; set; }

    /// <summary>
    /// 项目所在城市。参考行政区划字典表
    ///</summary>
    [Display(Name = "项目所在城市")]
    [Required(ErrorMessage = "{0}不能为空")]
    public int? CityCode { get; set; }

    /// <summary>
    /// 项目所在区县。参考行政区划字典表
    ///</summary>
    [Display(Name = "项目所在区县")]
    [Required(ErrorMessage = "{0}不能为空")]
    public int? AreaCode { get; set; }

    /// <summary>
    /// 实际开工日期
    ///</summary>
    [Display(Name = "实际开工日期")]
    [Required(ErrorMessage = "{0}不能为空")]
    public DateTime? StartDate { get; set; }

    /// <summary>
    /// 联系人姓名
    ///</summary>
    [Display(Name = "联系人姓名")]
    [Required(ErrorMessage = "{0}不能为空")]
    public string? LinkMan { get; set; }

    /// <summary>
    /// 联系人办公电话
    ///</summary>
    [Display(Name = "联系人办公电话")]
    [Required(ErrorMessage = "{0}不能为空")]
    public string? LinkPhone { get; set; }

    /// <summary>
    /// 位置坐标数
    ///</summary>
    [Display(Name = "位置坐标数")]
    [Required(ErrorMessage = "{0}不能为空")]
    public int? PositionCount { get; set; }

    /// <summary>
    /// 指工程建设项目中标合同价格，单位为元（不需要招投标的项目使用工程造价代替）
    ///</summary>
    [Display(Name = "中标合同价格")]
    [Required(ErrorMessage = "{0}不能为空")]
    public decimal? ContractAmount { get; set; }

    /// <summary>
    /// 中标通知书编号
    ///</summary>
    [Display(Name = "中标通知书编号")]
    public string? LetterAcceptanceNum { get; set; }

    /// <summary>
    /// 招标标段编号
    ///</summary>
    [Display(Name = "招标标段编号")]
    public string? BiddingSectionNum { get; set; }

    /// <summary>
    /// 指工程建设项目合同约定的项目开工日期
    ///</summary>
    [Display(Name = "指工程建设项目合同约定的项目开工日期")]
    [Required(ErrorMessage = "{0}不能为空")]
    public DateTime? ContractStartDate { get; set; }

    /// <summary>
    /// 指工程建设项目合同约定的项目竣工日期
    ///</summary>
    [Display(Name = "指工程建设项目合同约定的项目竣工日期")]
    [Required(ErrorMessage = "{0}不能为空")]
    public DateTime? ContractEndDate { get; set; }

    /// <summary>
    /// 中标通知书附件列表
    /// </summary>
    [Display(Name = "中标通知书附件列表")]
    public List<AttachmentsForm<int>> Attachments { get; set; } = new();
}

public static class ProjectUpdateApplyFormExceptions
{
    /// <summary>
    /// </summary>
    /// <param name="projectUpdateApply"></param>
    /// <returns></returns>
    public static ProjectUpdateApplyForm AsForm(this ProjectUpdateApply projectUpdateApply)
    {
        ProjectUpdateApplyForm form = new ProjectUpdateApplyForm();
        ServiceAccessor.GetService<IMapper>().Map(projectUpdateApply, form);
        var projectAttachmentService = ServiceAccessor.GetService<ProjectAttachmentService>();
        var attachments = projectAttachmentService.GetSysAttachmentByObjectIdAndType(projectUpdateApply.Id, 附件类型.中标通知书, 项目附件租户.ProjectUpdateApply);
        List<AttachmentsForm<int>> attach = new();
        foreach (var item in attachments)
        {
            AttachmentsForm<int> attachmentsForm = new()
            {
                AttachmentId = item.Id,
                AttachmentName = item.FriendlyName,
            };
            attach.Add(attachmentsForm);
        }
        form.Attachments = attach;

        return form;
    }

    public static List<AttachmentsForm<int>> GetAttachments(this ProjectUpdateApply projectUpdateApply)
    {
        var projectAttachmentService = ServiceAccessor.GetService<ProjectAttachmentService>();
        var attachments = projectAttachmentService.GetSysAttachmentByObjectIdAndType(projectUpdateApply.Id, 附件类型.中标通知书, 项目附件租户.ProjectUpdateApply);
        List<AttachmentsForm<int>> attach = new();
        foreach (var item in attachments)
        {
            AttachmentsForm<int> attachmentsForm = new()
            {
                AttachmentId = item.Id,
                AttachmentName = item.FriendlyName,
            };
            attach.Add(attachmentsForm);
        }
        return attach;
    }
}